package com.example.demo.Controller;

import com.example.demo.dao.GaoyuanRecord1Mapper;
import com.example.demo.dataobject.GaoyuanRecord1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Iterator;
import java.util.List;

@Controller
public class StudentController {
    @Autowired
    GaoyuanRecord1Mapper gaoyuanRecord1Mapper;//数据库

    @Autowired
    GaoyuanRecord1 gaoyuanRecord1;//列表


    @RequestMapping("get")//查询所有的信息
    public String getallmessage(ModelMap modelMap) {
        int b = 0;
        List<GaoyuanRecord1> gaoyuanRecord1s = gaoyuanRecord1Mapper.selectAll();
        for (GaoyuanRecord1 each : gaoyuanRecord1s) {

            each.setId(b + 1);
            b++;
        }
        modelMap.addAttribute("users", gaoyuanRecord1s);
        return "message";
    }

    @RequestMapping("/select1")
    public String sele() {
        return "select";
    }

    @RequestMapping("/modify")
    public String mod() {
        return "modify";
    }

    @RequestMapping("/modyfi")
    public String mod1() {
        return "modyfi";
    }

    @RequestMapping(path = "/modify1")
    private String modify1(@RequestParam("name") String name, ModelMap modelMap) {

        List<GaoyuanRecord1> gaoyuanRecord1ss = gaoyuanRecord1Mapper.selectAll();
        for (GaoyuanRecord1 gaoyuanRecord111 : gaoyuanRecord1ss) {

            if (gaoyuanRecord111.getName().equals(name)) {
                modelMap.addAttribute("name1", gaoyuanRecord111.getName());
                modelMap.addAttribute("age1", gaoyuanRecord111.getAge());
                modelMap.addAttribute("sex1", gaoyuanRecord111.getSex());
                modelMap.addAttribute("bir1", gaoyuanRecord111.getBirthday());
                modelMap.addAttribute("phone1", gaoyuanRecord111.getPhone());
                modelMap.addAttribute("email1", gaoyuanRecord111.getEmail());
                modelMap.addAttribute("address1", gaoyuanRecord111.getAddress());
                modelMap.addAttribute("profession1", gaoyuanRecord111.getProfession());
                return "modyfi";

            }
        }

        return "selectmessage";

    }


    @RequestMapping("/select")
    public String select(@RequestParam("name") String name, ModelMap modelMap) {
        List<GaoyuanRecord1> gaoyuanRecord1s = gaoyuanRecord1Mapper.selectAll();

        for (GaoyuanRecord1 each : gaoyuanRecord1s) {
            if (each.getName().equals(name)) {
                modelMap.addAttribute("name", each.getName());
                modelMap.addAttribute("age", each.getAge());
                modelMap.addAttribute("sex", each.getSex());
                modelMap.addAttribute("bir", each.getBirthday());
                modelMap.addAttribute("phone", each.getPhone());
                modelMap.addAttribute("email", each.getEmail());
                modelMap.addAttribute("address", each.getAddress());
                modelMap.addAttribute("profession", each.getProfession());

            }
        }
        return "selectmessage";
    }

    @RequestMapping("/add")
    public String get(GaoyuanRecord1 gaoyuanRecord1, ModelMap modelMap) {
        int a = 0;
        List<GaoyuanRecord1> gaoyuanRecord1s = gaoyuanRecord1Mapper.selectAll();
        if (gaoyuanRecord1 != null) {
            GaoyuanRecord1 gaoyuanRecord11 = gaoyuanRecord1Mapper.selectByName(gaoyuanRecord1.getName());
            if (gaoyuanRecord11 == null) {
                gaoyuanRecord1s.add(gaoyuanRecord1);
                for (GaoyuanRecord1 each : gaoyuanRecord1s) {
                    each.setId(a + 1);
                    a++;
                }
                gaoyuanRecord1Mapper.insert(gaoyuanRecord1);
            }
        }
        return "redirect:/get";
    }

    @RequestMapping("/delete1")
    public String delete(@RequestParam(value = "name", required = false) String name, ModelMap modelmap) {
        Iterator<GaoyuanRecord1> gaoyuanRecord1_GaoyuanRecord1 = gaoyuanRecord1Mapper.selectAll().iterator();
        while (gaoyuanRecord1_GaoyuanRecord1.hasNext()) {
            GaoyuanRecord1 a = gaoyuanRecord1_GaoyuanRecord1.next();
            if (a.getName().equals(name)) {
                gaoyuanRecord1_GaoyuanRecord1.remove();
                gaoyuanRecord1Mapper.deleteByAll(name);
            }
        }
        int b = 0;
        List<GaoyuanRecord1> gaoyuanRecord1ss = gaoyuanRecord1Mapper.selectAll();

        if (gaoyuanRecord1ss != null) {
            for (GaoyuanRecord1 each : gaoyuanRecord1ss) {
                each.setId(b + 1);
                b++;
            }
            modelmap.addAttribute("stu1", gaoyuanRecord1ss);

        }
        return "redirect:/get";
    }


    @RequestMapping("/modyy")
    public String mody(@RequestParam("name") String name,
                       @RequestParam("sex") String sex,
                       @RequestParam("age") String age,
                       @RequestParam("address") String address,
                       @RequestParam("profession") String profession,
                       @RequestParam("email") String email,
                       @RequestParam("bir") String bir,
                       @RequestParam("phone") String phone) {
        List<GaoyuanRecord1> list = gaoyuanRecord1Mapper.selectAll();

        for (GaoyuanRecord1 i : list) {


            if (i.getName().equals(name)) {

                if (!i.getAge().equals(age)) {
                    gaoyuanRecord1Mapper.updateByPrimaryKey(name, age);
                }
                if (!i.getAddress().equals(address)) {
                    gaoyuanRecord1Mapper.updateByPrimaryKey1(name, address);
                }
                if (!i.getProfession().equals(profession)) {
                    gaoyuanRecord1Mapper.updateByPrimaryKey2(name, profession);
                }

                if (!i.getEmail().equals(email)) {
                    gaoyuanRecord1Mapper.updateByPrimaryKey3(name, email);
                }

                if (!i.getPhone().equals(phone)) {
                    gaoyuanRecord1Mapper.updateByPrimaryKey4(name, phone);
                }

                if (!i.getBirthday().equals(bir)) {
                    gaoyuanRecord1Mapper.updateByPrimaryKey5(name, bir);
                }


            }
        }
        return "redirect:/get";
    }


    @RequestMapping("/message")
    public String add1() {
        return "messageadd";
    }


    @RequestMapping("/deletel")
    public String delet() {
        return "deletemessage";
    }


}
